<template>
  <el-card shadow="navar">
    <el-row :gutter="15">
      <el-form ref="elForm" :model="formData" size="medium" label-width="120px">
        <el-col :span="12">
          <el-form-item label="活动名称" prop="peerTitle">
            <el-input
              v-model.trim="formData.peerTitle"
              placeholder="请输入活动名称"
              :disabled="disabled"
              :maxlength="50"
              show-word-limit
              clearable
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动状态" prop="status">
            <el-select
              v-model="formData.status"
              placeholder="请选择活动状态"
              :disabled="disabled"
              clearable
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="(item, index) in dict.type.activity_status"
                :key="index"
                :label="item.label"
                :value="parseInt(item.value)"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动开始时间" prop="timeStart">
            <el-date-picker
              v-model.trim="formData.timeStart"
              type="datetime"
              placeholder="请选择开始时间"
              :disabled="disabled"
              :style="{ width: '100%' }"
              default-time="0:00:00"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动结束时间" prop="timeEnd">
            <el-date-picker
              v-model.trim="formData.timeEnd"
              type="datetime"
              placeholder="请选择结束时间"
              :disabled="disabled"
              :style="{ width: '100%' }"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="虚拟报名人数量" prop="inventedJoinNumber">
            <el-input
              v-model.number="formData.inventedJoinNumber"
              placeholder="请输入报名人数"
              :disabled="disabled"
              clearable
              :style="{ width: '100%' }"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邀请设置" prop="invite">
            <el-radio-group v-model="formData.invite" :disabled="disabled">
              <el-radio
                v-for="(item, index) in dict.type.activity_invite"
                :key="index"
                :label="parseInt(item.value)"
                >{{ item.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="适用单位" prop="applicableUnitNo">
            <el-select
              v-model="formData.applicableUnitNo"
              :disabled="disabled"
              multiple
              placeholder="请选择适用单位"
              clearable
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="item in applicableUnitOptions"
                :key="item.applicableUnitNo"
                :label="item.applicableUnit"
                :value="item.applicableUnitNo"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-divider></el-divider>
          <el-form-item label="添加同行商品" prop="activitySeckillList">
            <el-table
              style="margin-top: 10px"
              :data="formData.activitySeckillList"
            >
              <el-table-column label="类型" align="center" prop="peerType"
                ><template slot-scope="scope">
                  {{
                    scope.row.peerType
                      | typeFilter(dict.type.activity_goodsType)
                  }}
                </template>
              </el-table-column>
              <el-table-column label="实物照片" align="center" prop="peerUrl">
                <template slot-scope="scope">
                  <el-image
                    style="width: 60px; height: 60px"
                    :src="scope.row.peerUrl"
                    fit="cover"
                  ></el-image>
                </template>
              </el-table-column>
              <el-table-column
                label="奖品名称"
                align="center"
                prop="peerName"
              ></el-table-column>
              <el-table-column
                label="组队人数"
                align="center"
                prop="peerNumber"
              ></el-table-column>
              <el-table-column
                label="赠送人数"
                align="center"
                prop="peerGive"
              ></el-table-column>
              <el-table-column
                label="奖品库存"
                align="center"
                prop="peerStock"
              ></el-table-column>
            </el-table>
          </el-form-item>
          <el-divider></el-divider>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动封面" prop="coverImage">
            <el-image
              v-for="(item, index) in formData.coverImage.split(',')"
              :key="index"
              :src="getImgUrl(item)"
              lazy
              class="img"
              fit="contain"
            ></el-image>
          </el-form-item>
          <el-form-item label="分享封面" prop="shareImage">
            <el-image
              v-for="(item, index) in formData.shareImage.split(',')"
              :key="index"
              :src="getImgUrl(item)"
              lazy
              class="img"
              fit="contain"
            ></el-image>
          </el-form-item>
          <el-form-item label="活动描述" prop="peerContent">
            <editor
              v-model="formData.peerContent"
              :min-height="192"
              :readOnly="disabled"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item size="large">
            <el-button @click="goBack">取消</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </el-card>
</template>

<script>
import { getPeerById } from '@/api/activity/peer'
import { applicableUnitList } from '@/api/coupon/manage'
export default {
  props: [],
  dicts: ['activity_status', 'activity_invite', 'activity_goodsType'],
  data() {
    return {
      disabled: true,
      //门店数据
      applicableUnitOptions: [],
      formData: {
        peerTitle: '',
        status: null,
        timeStart: '',
        timeEnd: '',
        inventedJoinNumber: null,
        invite: 1,
        coverImage: '',
        shareImage: '',
        activitySeckillList: [],
        applicableUnitNo: [],
      },
    }
  },
  filters: {
    typeFilter(val, type) {
      console.log(val)
      console.log(type)
      if (val === null) return null
      let data = type.filter((item) => {
        return val == item.value
      })
      if (data.length) {
        return data[0].label
      } else {
        return val
      }
    },
  },
  mounted() {
    Promise.all([this.getApplicableUnitList()]).then(([res]) => {
      if (this.$route.params.id) {
        this.$nextTick(() => {
          this.getPeerDetail()
        })
      }
    })
  },
  methods: {
    // 获取同行活动详情
    getPeerDetail() {
      getPeerById({ id: this.$route.params.id }).then((res) => {
        this.formData = res.data
        this.formData.applicableUnitNo = res.data.activityUnit.map((item) => {
          return item.applicableUnitNo
        })
      })
    },
    // 获取活动门店
    getApplicableUnitList() {
      applicableUnitList().then((res) => {
        this.applicableUnitOptions = res.data
      })
    },
    goBack() {
      this.$router.go(-1) // 返回
      //关闭子页面
      this.$store.state.tagsView.visitedViews.splice(
        this.$store.state.tagsView.visitedViews.findIndex(
          (item) => item.path === this.$route.path
        ),
        1
      )
      this.$router.push(
        this.$store.state.tagsView.visitedViews[
          this.$store.state.tagsView.visitedViews.length - 1
        ].path
      )
    },
  },
}
</script>
<style scoped>
.img {
  height: 254px;
  border: 1px solid #ddd;
  display: block;
  margin-bottom: 15px;
}
</style>